<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./site/public/im.php, $Author:  $ */

    global $_CONF;
    $_CONF['debug'] = 0;
    
    $oSrvChat = &App::getModuleService('Chat', 'Chat');
    $oSrvSec = &App::getModuleService('Account', 'Security');
    $oReq = &App::getRequest();
    if(App::getSetting('check_im'))
        App::gotoUrl('public');
    $oTxt = App::loadClass('Text');
    $oTxt = new Text();
    $oUser = $oSrvSec->getCurrentUser();
    $iChatId = $oReq->getInt('chat');
    $mXmlApend = false;
    
    $oSrvChat->setUserSession($oUser->aData['user'], $iChatId);
    
    if(!$oSrvChat->getCurrentSession($iChatId))
    {
        $mXmlApend = true;
    }
    
    if($oReq->get('proc'))
    {
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    	header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT");
    	header("Cache-Control: no-cache, must-revalidate" );
    	header("Pragma: no-cache");
    	header("Content-Type: text/xml; charset=utf-8");
    	
    	if($oReq->get('message'))
    	{
    		$sTxt = str_replace("\n"," ",$oReq->get('message'));
            $oSrvChat->saveChatMessage($oUser->aData['user'], $sTxt, $iChatId);
    	}
        
    	$sXml = '<?xml version="1.0"?><root>';
    	$aChat = $oSrvChat->getChatMessage($iChatId, $oReq->getInt('last'));
    	
    	foreach ($aChat as $i=>$aData)
    	{
    		$sXml .= '<message id="'.$aData['message_id'].'">';
    		$sXml .= '<user>'.htmlspecialchars($aData['user_name']).'</user>';
    		$sXml .= '<text>'.htmlspecialchars($oTxt->parseText($aData['message'])).'</text>';
    		$sXml .= '<time>'.$aData['post_time'].'</time>';
    		$sXml .= '</message>';
    	}
    	
    	if($mXmlApend)
    	{
    		$sXml .= '<message id="final">';
    		$sXml .= '<user>System</user>';
    		$sXml .= '<text>User has quit</text>';
    		$sXml .= '<time></time>';
    		$sXml .= '</message>';
    	}
    	$sXml .= '</root>';

    	echo $sXml;
    	exit;
    }
    
?>